﻿using FictionsDownload.BLL;
using FictionsDownload.Model;
using FictionsDownload.Static;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FictionsDownload.UI.PopDialog
{
    public partial class FormWebFormatEdit : Form
    {
        private WebFormatModel webFormatModel;
        private WebFormat webFormat = new WebFormat();
        private EditState editState;
        private SortOrder SortOrder
        {
            get
            {
                foreach (RadioButton item in grBox_Sort.Controls)
                {
                    if (item.Checked)
                    {
                        if (item.Text == "升序")
                        {
                            return SortOrder.Ascending;
                        }
                        else if (item.Text == "降序")
                        {
                            return SortOrder.Descending;
                        }
                        else
                        {
                            return SortOrder.None;
                        }
                    }
                }
                return SortOrder.None;
            }
            set
            {
                string text;
                if (value == SortOrder.Ascending)
                {
                    text = "升序";
                }
                else if (value == SortOrder.Descending)
                {
                    text = "降序";
                }
                else
                {
                    text = "不排序";
                }
                foreach (RadioButton item in grBox_Sort.Controls)
                {
                    if (item.Text == text)
                    {
                        item.Checked = true;
                    }
                    else
                    {
                        item.Checked = false;
                    }
                }
            }
        }
        public FormWebFormatEdit(EditState editState,string id)
        {
            this.editState = editState;
            if(editState == EditState.新建)
            {
                webFormatModel = new WebFormatModel();
            }
            else if (id == null)
            {
                MessageBox.Show("修改状态，必须传入Id");
                Dispose();
            }
            else
            {
                webFormatModel = webFormat.Get<WebFormatModel>(w=>w.Id == id);
            }
            InitializeComponent();
            InitializeComponentCode();
        }
        private void InitializeComponentCode()
        {
            Text = editState.ToString() + Text;
            if(editState == EditState.修改)
            {
                txt_Name.Text = webFormatModel.Name;
                txt_Url.Text = webFormatModel.Url;
                txt_HtmlHead.Text = webFormatModel.HtmlHead;
                txt_HtmlTail.Text = webFormatModel.HtmlTail;
                txt_VolumeHead.Text = webFormatModel.VolumeHead;
                txt_VolumeTail.Text = webFormatModel.VolumeTail;
                txt_TextHead.Text = webFormatModel.TextHead;
                txt_TextTail.Text = webFormatModel.TextTail;
                this.SortOrder = webFormatModel.SortOrder;
            }
        }

        private void btn_Accept_Click(object sender, EventArgs e)
        {
            webFormatModel.Name = txt_Name.Text;
            webFormatModel.Url =  HttpHelper.UrlParse(txt_Url.Text,true);
            webFormatModel.HtmlHead = txt_HtmlHead.Text;
            webFormatModel.HtmlTail = txt_HtmlTail.Text;
            webFormatModel.VolumeHead = txt_VolumeHead.Text;
            webFormatModel.VolumeTail = txt_VolumeTail.Text;
            webFormatModel.TextHead = txt_TextHead.Text;
            webFormatModel.TextTail = txt_TextTail.Text;
            webFormatModel.SortOrder = this.SortOrder;
            bool bl;
            if (editState == EditState.新建)
            {
                bl = webFormat.Add(webFormatModel);
            }
            else
            {
                bl = webFormat.Update(webFormatModel);
            }
            if (bl)
            {
                webFormat.SaveChanges();
                Dispose();
            }
            else
            {
                MessageBox.Show(editState.ToString() + "失败:" + webFormat.Message);
            }
        }
    }
}
